package com.menu.Controller;

import com.menu.Service.GoodsService;
import com.menu.Service.OrderService;
import com.menu.Utils.CommonUtil;
import com.menu.Utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


@Api(tags = "商品接口")
@RestController
@RequestMapping("/Visitor")
public class GoodsController {

    @Autowired
    private CommonUtil commonUtil;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;

    @ApiOperation("根据商品id查找")
    @GetMapping("/selectGoodsById")
    public ResponseResult selectGoodsById(Integer goodsId,@ApiIgnore HttpServletRequest httpRequest){
        if (goodsId == null){
            return ResponseResult.error("参数有误！");
        }
        return goodsService.selectGoodsById(goodsId,0,httpRequest);
    }

    @ApiOperation("商品分页查询")
    @GetMapping("/selectGoodsByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classfify",value = "商品分类,为空的时候标识默认")
    })
    public ResponseResult selectGoodsByPage(Integer pageStart,Integer pageSize,String classfify){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (!commonUtil.verifyClassfy(classfify)){
            classfify = null;
        }
        return goodsService.selectGoodByPage(pageStart,pageSize,0,classfify);
    }

    @ApiOperation("商品分页查询")
    @GetMapping("/selectGoods")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName",value = "商品名称")
    })
    public ResponseResult selectGoods(Integer pageStart,Integer pageSize,String goodsName){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (!commonUtil.verifyStirng(goodsName)){
            return ResponseResult.error("商品名称！");
        }
        return goodsService.visualGoods(goodsName,pageStart,pageSize,0);
    }

    @ApiOperation("模糊搜索")
    @GetMapping("/visualGoods")
    public ResponseResult visualGoods(Integer pageStart,Integer pageSize,String Information){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (!commonUtil.verifyStirng(Information)){
            return ResponseResult.error("参数为空！");
        }
        return goodsService.visualGoods(Information,pageStart,pageSize,0);
    }

    @ApiOperation("商品排行")
    @GetMapping("/commodityList")
    public ResponseResult commodityList(){
        return orderService.choicenessList();
    }
}
